<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_V2_Hotel extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Hotel
     */
    private $validation;

    /**
     * 酒店服务
     * @var Service_Api_Hotel
     */
    private $hotel_service = null;

    public function before()
    {
        parent::before();

        // 返回数据
        $this->result = array(
            'status' => true,//默认状态为成功
            'msg' => '操作成功',//默认的提示消息
            'data' => array()
        );
        // 验证类实例化
        $this->validation = Request_Api_V2_Factory::create('hotel', $this->request_params);
        //酒店服务
        $this->hotel_service = new Service_Api_Hotel();
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 酒店详情
    public function action_get_detail()
    {
        try {
            $this->validation->get_detail()->check();
            $params['id']         = $this->request_params['product_id'];
            $params['mid']        = $this->mid;
            $params['is_seckill'] = $this->request_params['is_seckill'];
            $params['platform_no'] = $this->platform_no;
            $data                 = $this->hotel_service->info($params);
            if (empty($data)) {
                throw new Exception('查询酒店失败');
            }
            $this->result['data'] = $data;
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 单酒店详细信息，默认升序查一个
    public function action_single_hotel_detail()
    {
        try {
            $params               = [];
            $params['userid']     = $this->mid;
            $params['is_seckill'] = $this->request_params['is_seckill'];
            $params['platform_no'] = $this->request_params['platform_no'];
            $this->result['data'] = $this->hotel_service->get_hotel_by_order($params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 创建订单
    public function action_add_order()
    {
        try {
            // 验证
            $this->validation->add_order()->check();
            $this->request_params['member_info'] = $this->member_info;

            $this->result['data'] = $this->hotel_service->create_order($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 首页当季推荐
    public function action_get_index_recommend()
    {
        try {
            $this->validation->get_index_recommend()->check();
            $pagesize             = Arr::get($this->request_params, 'pagesize', 4);
            $this->result['data'] = $this->hotel_service->get_index_recommend($pagesize,$this->platform_no);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 查询条件
    public function action_get_query_condition()
    {
        $this->result['data'] = $this->hotel_service->query_condition();
    }

    // 酒店查询接口
    public function action_get_general_query()
    {
        try {
            $this->validation->get_general_query()->check();
            $pagesize = $this->request_params['pagesize'] ?? 10;
            $params   = array(
                'page' => $this->request_params['page'],
                'pagesize' => $pagesize,
                'keyword' => $this->request_params['keyword'],
                'starttime' => strtotime($this->request_params['start_time']),
                'endtime' => strtotime($this->request_params['end_time']),
                'destpy' => $this->request_params['destpy'],
                'rankid' => $this->request_params['rank_id'],
                'priceid' => $this->request_params['price_id'],
                'sorttype' => $this->request_params['sorttype'],
                'attrid' => $this->request_params['attrid'],
                'platform_no' => $this->platform_no,
                'member_id' => $this->mid
            );

            $hot_attrid = $this->request_params['hot_attrid'];
            if ($hot_attrid) {
                $attrid = explode('_', $params['attrid']);
                array_push($attrid, $hot_attrid);
                $params['attrid'] = implode('_', array_unique($attrid));
            }
            //经度,纬度(周边酒店搜索)
            if ($this->request_params['geo']) {
                $geo                = json_decode($this->request_params['geo'], true);
                $lat                = (float)$geo['lat'];
                $lng                = (float)$geo['lng'];
                $distance           = Model_Sysconfig::get_configs(0, array('cfg_plugin_hotel_map_distance'), 0)['cfg_plugin_hotel_map_distance'];
                $distance           = $distance ?: 4000;
                $params['lat']      = $lat;
                $params['lng']      = $lng;
                $params['distance'] = $distance;
            }
            $result               = $this->hotel_service->search($params);
            if($this->request_params['geo']){
                $result['distance'] = $distance;
            }
            $this->result['data'] = $result;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取游客信息是否开启
    public function action_get_tourists()
    {
        $this->result['data'] = $this->hotel_service->get_tourer_config();
    }

    // 根据酒店目的地获取酒店列表
    public function action_get_dest_list()
    {
        try {
            $this->validation->get_dest_list()->check();
            $dest_id              = $this->request_params['dest_id'];
            $page                 = $this->request_params['page'];
            $pagesize             = $this->request_params['pagesize'];
            $this->result['data'] = $this->hotel_service->get_hotel_list_by_dest($dest_id, $page, $pagesize,$this->platform_no);

        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获得星级和价格
    public function action_get_rp()
    {
        $this->result['data'] = $this->hotel_service->get_rand_and_price();
    }

    // 根据日期获得该酒店套餐列表
    public function action_get_day_suitlist()
    {
        try {
            $this->validation->get_day_suitlist()->check();
            $this->request_params['mid'] = $this->mid;
            $this->result['data']        = $this->hotel_service->get_suit_list_by_day($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取报价
    public function action_get_everyday_price()
    {
        try {
            $this->validation->get_everyday_price()->check();
            $this->request_params['mid'] = $this->mid;
            $this->result['data']        = $this->hotel_service->get_everyday_price($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }

    }

    // 获取房型信息
    public function action_hotel_room()
    {
        try {
            $this->validation->hotel_room()->check();
            $suit_id              = $this->request_params['suit_id'];
            $hotel_id             = $this->request_params['hotel_id'];
            $seckill_id           = $this->request_params['is_seckill'];//秒杀id
            $this->result['data'] = $this->hotel_service->get_room_detail($hotel_id, $suit_id, $seckill_id);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取热门酒店属性
    public function action_get_hot_attr()
    {
        $this->result['data'] = $this->hotel_service->get_hotel_attr();
    }

    // 获取酒店热门推荐列表
    public function action_get_hot_hotel()
    {
        try {
            $this->validation->get_hot_hotel()->check();
            $page                               = Arr::get($this->request_params, 'page', 1);
            $pagesize                           = Arr::get($this->request_params, 'pagesize', 10);
            $this->result['data']['hotel_list'] = $this->hotel_service->get_hot_hotel($page, $pagesize, $this->mid,$this->platform_no);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}